package com.volunteer.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.volunteer.common.R;
import com.volunteer.entity.Carousel;
import com.volunteer.service.CarouselService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图控制器
 */
@Tag(name = "轮播图管理")
@Slf4j
@RestController
@RequestMapping("/carousel")
@RequiredArgsConstructor
public class CarouselController {

    private final CarouselService carouselService;

    /**
     * 创建轮播图
     */
    @Operation(summary = "创建轮播图")
    @PostMapping("/create")
    @SaCheckRole("admin")
    public R<Boolean> createCarousel(@RequestBody @Validated Carousel carousel) {
        boolean result = carouselService.createCarousel(carousel);
        return R.ok(result);
    }

    /**
     * 更新轮播图
     */
    @Operation(summary = "更新轮播图")
    @PostMapping("/update")
    @SaCheckRole("admin")
    public R<Boolean> updateCarousel(@RequestBody @Validated Carousel carousel) {
        boolean result = carouselService.updateCarousel(carousel);
        return R.ok(result);
    }

    /**
     * 删除轮播图
     */
    @Operation(summary = "删除轮播图")
    @PostMapping("/delete/{id}")
    @SaCheckRole("admin")
    public R<Boolean> deleteCarousel(@PathVariable Long id) {
        boolean result = carouselService.deleteCarousel(id);
        return R.ok(result);
    }

    /**
     * 查询所有启用的轮播图
     */
    @Operation(summary = "查询所有启用的轮播图")
    @GetMapping("/list")
    @SaIgnore
    public R<List<Carousel>> listCarousels() {
        List<Carousel> list = carouselService.listEnabledCarousels();
        return R.ok(list);
    }

    /**
     * 分页查询轮播图
     */
    @Operation(summary = "分页查询轮播图")
    @GetMapping("/page")
    @SaCheckRole("admin")
    public R<Page<Carousel>> pageCarousels(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") long current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") long size,
            @Parameter(description = "状态：0-禁用，1-启用") @RequestParam(required = false) Integer status,
            @Parameter(description = "标题") @RequestParam(required = false) String title,
            @Parameter(description = "活动ID") @RequestParam(required = false) Long activityId
    ) {
        Page<Carousel> page = new Page<>(current, size);
        Carousel carousel = new Carousel();
        carousel.setStatus(status);
        carousel.setTitle(title);
        carousel.setActivityId(activityId);
        
        Page<Carousel> result = carouselService.pageCarousels(page, carousel);
        return R.ok(result);
    }

    /**
     * 启用/禁用轮播图
     */
    @Operation(summary = "启用/禁用轮播图")
    @PostMapping("/status/{id}/{status}")
    @SaCheckRole("admin")
    public R<Boolean> setCarouselStatus(
            @Parameter(description = "轮播图ID") @PathVariable Long id,
            @Parameter(description = "状态：0-禁用，1-启用") @PathVariable Integer status
    ) {
        boolean result = carouselService.setCarouselStatus(id, status);
        return R.ok(result);
    }
} 